home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 416 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  95 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: weber@ezibk8.vmsmail.ethz.ch
  3. Newsgroups: comp.std.c++
  4. Subject: virtual base class initialization
  5. Date: 21 Feb 1996 09:51:38 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <199602211555.IAA16749@ncar.ucar.EDU>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: Wed, 21 Feb 1996 16:54:27 +0100
  11. X-Vms-To: ETHZ::"std-c++@ncar.ucar.edu"
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMStcL0y4NqrwXLNJAQFDNAH+OIJK56J+kDh4RyDU/daSDyZxTYnO/vLU
  14.     /Fah8plcsqSi58xOSx7/iZ2hZF3YVy96xGJp7RYXfTpI60SFYAsprw==
  15.     =nAFb
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. // 
  19. // I am not sure how to interprate the ARM (or the Draft C++ Standard) 
  20. // with regard to the initialization of a virtual base class. I have tried 
  21. // some examples on a DEC C++ compiler, but I am not sure whether the results 
  22. // are o.k. Could anybody give me some details?
  23. //
  24. // Benedikt Weber, Swiss Federal Institute of Technology, Zurich
  25. // weber@ibk.baum.ethz.ch
  26. //*************************************************************************
  27. // Here some problems initializing a virtual base class V
  28. // The results indicated were obtained with the DEC C++ compiler
  29.  
  30. #include <iostream.hxx>
  31.  
  32. class V{
  33. public:
  34.     V(int i) {cout << "init V(int)" << endl;}
  35.     V() {cout << "init V()" << endl;}
  36. };
  37.  
  38. class A: virtual public V{
  39. public:
  40.     A() {}
  41.     A(int i): V(i) {}
  42. };
  43.  
  44. class B: virtual public V{
  45. };
  46.  
  47.  
  48. class C1: public A, public B, virtual public V{
  49. public:
  50.     C1(int i): A(i) {}
  51. };
  52.  
  53. // C1 is similar to the ARM example at the end of Section 12.6.2
  54. // and works correctly. V is initialized directly by the most derived class
  55. // C1 as V(). My example prints correctly "init V()"
  56.  
  57.  
  58. class C2: public A, public B{
  59. public:
  60.     C2(int i): A(i){}
  61. };
  62.  
  63. // C2 is not directly derived from V. In this case I don't think the base
  64. // class should still be initialized by the most derived class. Rather
  65. // C2 should initialize A(i) which in turn sould initialize V(i).
  66. // This would be in accordance with the example in the ARM in Section 12.6.2
  67. // where the order of initializatoin is explained. However, the ARM is not
  68. // quite clear in this point and it could be a question of interpretation.
  69. // 
  70. // My example prints "init V()", i.e. it initializes V directly from
  71. // C2, which seems not correct to me.
  72. //
  73.  
  74. class C3: public A, public B{
  75. public:
  76.     C3(int i): V(i){}
  77. };
  78.  
  79. // C3 is not directly derived from V. Nevertheless it does initialze V(i)
  80. // and the example prints "init V(int)". I wonder whether this is correct
  81. // because I think a class can only initialize it's direct base classes.
  82.  
  83. int main(){
  84.     C1(1);
  85.     C2(2);
  86.     C3(3);
  87. }
  88. ---
  89. [ To submit articles: Try just posting with your newsreader.  If that fails,
  90.                       use mailto:std-c++@ncar.ucar.edu
  91.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  92.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  93.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  94. ]
  95.